package com.mall.biz.weishang;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.custom.dao.write.UserExtendedWriteMapper;
import com.mall.custom.dao.write.UserWriteMapper;
import com.mall.enums.UserEnum;
import com.mall.pojo.User;
import com.mall.pojo.UserExtended;
import com.mall.vo.user.UserVO;

@Service
public class WeishangUserBizImpl {
	private Logger log = LoggerFactory.getLogger(WeishangUserBizImpl.class);
	@Autowired
	private UserWriteMapper userWriteMapper;
	@Autowired
	private UserExtendedWriteMapper userExtendedWriteMapper;
	
	public int weishangRegister(UserVO userVO, Date createTime) {
		User user = userVO.getUser();
		UserExtended userExtended = userVO.getUserExtended();
		if (user == null) {
			log.error("register error,user is null");
			return 0;
		}
		user.setGmtCreate(createTime);
		user.setGmtModify(new Date());
		user.setIntegral(0l);// 初始积分为0
		user.setSex(Short.parseShort("0"));// 默认性别为男
		user.setType(UserEnum.FENXIAOSHANG.getCode());// 区分为普通用户
		user.setLevel(Short.parseShort("0"));// 默认为最低等级0的分销商
		user.setStatus(Short.parseShort("1"));
		if (user.getStatus() == null) {
			user.setStatus(Short.parseShort("1"));
		}

		Integer result = null;  
		result = this.userWriteMapper.insert(user);// 插入user表
		if (userExtended == null) {
			log.error("register error,userExtended is null");
			return 0;
		}
		userExtended.setUserId(user.getId());
		userExtended.setGmtCreate(createTime);
		userExtended.setGmtModify(new Date());
		userExtended.setBalance(0l);
		userExtended.setBond(0l);
		userExtended.setStatus(0);
		userExtended.setType(0);
		result = this.userExtendedWriteMapper.insert(userExtended);
		return result;
	}
}
